﻿using MvcApplication.Common;
using MvcApplication.CommonWebApi.Domain;
using MySql.Data.MySqlClient;
using System.Data.Common;

namespace MvcApplication.CommonWebApi
{
    public class ConnectionFactory
    {
        public static DbConnection GetConnection(int connId)
        {
            var connInfo = ConnCollection.Get(connId);
            switch (connInfo.Type)
            {
                case "MySql":
                    return new MySqlConnection(connInfo.ConnString);

                default:
                    throw new BizException("不支持的数据库类型");
            }
        }

        public static DbTransaction GetTransaction(DbConnection connection)
        {
            if (connection is MySqlConnection)
            {
                return connection.BeginTransaction();
            }

            throw new BizException("不支持的数据库类型");
        }
    }
}
